/*
 * camera.c
 *
 *  Created on: 2019-7-20
 *      Author: Administrator
 */

#include "regs.h"
#include "camera.h"

void camera0_write_i2c(unsigned short addr,unsigned char data){

	unsigned int  nTempVal =0;

	nTempVal = (0x1 << 31|addr << 8|data);
	rSCCB0_CTRCL = nTempVal;
    while(nTempVal&(0x1 << 31)){
    	nTempVal = rSCCB0_CTRCL;
    }
    udelay(10000);
}

void camera0_read_i2c(unsigned short addr){

	unsigned int  nTempVal =0;

	nTempVal = (0x1 << 31|0x1 << 24|addr << 8|0x0);
	rSCCB0_CTRCL = nTempVal;
    while(nTempVal&(0x1 << 31)){
    	nTempVal = rSCCB0_CTRCL;
    }
    nTempVal = rSCCB0_READ&0xff;
    udelay(1000000);

    xil_printf("camera0 addr[%2x] read chip_id=%2x\n\r",addr,nTempVal);
}

void camera1_write_i2c(unsigned short addr,unsigned char data){

	unsigned int  nTempVal =0;

	nTempVal = (0x1 << 31|addr << 8|data);
	rSCCB1_CTRCL = nTempVal;
    while(nTempVal&(0x1 << 31)){
    	nTempVal = rSCCB1_CTRCL;
    }
    udelay(10000);

}


void camera0_read_id(){

	unsigned int  nTempVal =0;
	unsigned int  chip_id  =0;

	nTempVal = (0x1 << 31|0x1 << 24|0x300a << 8|0x0);
	rSCCB0_CTRCL = nTempVal;
    while(nTempVal&(0x1 << 31)){
    	nTempVal = rSCCB0_CTRCL;
    }

    nTempVal = rSCCB0_READ;
    chip_id  = nTempVal&0xff;
    udelay(1000000);

    nTempVal = (0x1 << 31|0x1 << 24|0x300b << 8|0x0);
    rSCCB0_CTRCL = nTempVal;
    while(nTempVal&(0x1 << 31)){
    	nTempVal = rSCCB0_CTRCL;
    }

    nTempVal = rSCCB0_READ;
    chip_id  = chip_id << 8;
    chip_id |= nTempVal&0xff;

    xil_printf("camera0 verify chip_id=%2x\n\r",chip_id);
}

void camera1_read_id(){

	unsigned int  nTempVal =0;
	unsigned int  chip_id  =0;

	nTempVal = (0x1 << 31|0x1 << 24|0x300a << 8|0x0);
	rSCCB1_CTRCL = nTempVal;
    while(nTempVal&(0x1 << 31)){
    	nTempVal = rSCCB1_CTRCL;
    }

    nTempVal = rSCCB1_READ;
    chip_id  = nTempVal&0xff;
    udelay(1000000);

    nTempVal = (0x1 << 31|0x1 << 24|0x300b << 8|0x0);
    rSCCB1_CTRCL = nTempVal;
    while(nTempVal&(0x1 << 31)){
    	nTempVal = rSCCB1_CTRCL;
    }

    nTempVal = rSCCB1_READ;
    chip_id  = chip_id << 8;
    chip_id |= nTempVal&0xff;

    xil_printf("camera1 verify chip_id=%2x\n\r",chip_id);
}

void camera_status_show(){
	unsigned int i0,i1;
	unsigned int j0,j1;

	i0 = (rCAM0_PARAM&0xff000000) >> 24;
	i1 = rCAM0_PARAM&0x00ffffff;
	j0 = (rCAM1_PARAM&0xff000000) >> 24;
	j1 = rCAM1_PARAM&0x00ffffff;

    printf("camera0 err=%d frame rate=%d data count=%d ,camera1 err=%d frame rate=%d data count=%d . \n\r",rCAM0_ERR,i0,i1,rCAM1_ERR,j0,j1);
}

